package vn.edu.saigontech.ncms.web.interceptor;

import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;

import vn.edu.saigontech.ncms.model.user.LoggingRecord;
import vn.edu.saigontech.ncms.service.LoggingRecordService;
import vn.edu.saigontech.ncms.web.BaseAction;
import vn.edu.saigontech.ncms.web.BaseLogging;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

@SuppressWarnings("serial")
public class LoggingInterceptor extends AbstractInterceptor {
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		BaseAction action = (BaseAction) invocation.getAction();
		String result = invocation.invoke();
		List<BaseLogging> loggings = action.getLoggings();
		if (loggings != null){
			for(BaseLogging logging : loggings) {
				LoggingRecord record = new LoggingRecord();
				PropertyUtils.copyProperties(record, logging);
				LoggingRecordService.INSTANCE.persist(record);
			}
		}
		return result;
	}
}
